//
//  TVCouponsViewController.swift
//  iPhone
//
//  Created by Toj on 12/1/22.
//

import UIKit

class TVCouponsViewController: UIViewController {

    var coupons: [TVCouponCellModel] = [] { didSet {
        tableView.reloadData()
    } }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("-->toj: " + #function)
        var frame = contentView.frame
        frame.origin.y = view.frame.height*0.3+6
        UIView.animate(withDuration: 0.25) {
            self.contentView.frame = frame
        }
    }
    
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var headerView: TVCounponHeaderView = {
       let view = TVCounponHeaderView()
        view.backgroundColor = .white
        view.delegate = self
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.contentInsetAdjustmentBehavior = .never
        view.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
        view.separatorStyle = .none
        view.backgroundColor = .white
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.rowHeight = 94
        
        view.register(TVCouponCell.cell_class, forCellReuseIdentifier: TVCouponCell.cell_identifier)
        
        view.dataSource = self
        return view
    }()
}

extension TVCouponsViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return coupons.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: TVCouponCell.cell_identifier, for: indexPath) as? TVCouponCell
        if cell == nil {
            cell = TVCouponCell(style: .default, reuseIdentifier: TVCouponCell.cell_identifier)
        }
        cell?.coupon = coupons[indexPath.row]
        
        return cell!
    }
}

extension TVCouponsViewController: TVCounponHeaderViewDelegate {
    func counponHeaderViewDidTapClose(_ view: TVCounponHeaderView) {
        var frame = self.view.frame
        frame.origin.y = CGFloat.sr_height
        UIView.animate(withDuration: 0.25) {
            self.contentView.frame = frame
        } completion: { success in
            self.contentView.isHidden = true
            self.dismiss(animated: false, completion: nil)
        }
    }
}

// MARK: - 初始化
extension TVCouponsViewController {
    
    private func setupMakeAddSubviews() {
        view.addSubview(contentView)
        contentView.addSubview(tableView)
        contentView.addSubview(headerView)
    }
    
    private func setupMakeLayoutSubviews() {
        let frame = CGRect(x: 0, y: view.frame.height, width: view.frame.width, height: view.frame.height*0.7)
        contentView.frame = frame
        
        headerView.snp.makeConstraints { make in
            make.height.equalTo(105.ui_scale)
            make.top.left.right.equalTo(contentView)
        }
        tableView.snp.makeConstraints { make in
            make.left.right.equalTo(contentView)
            make.top.equalTo(headerView.snp.bottom)
            make.bottom.equalTo(contentView)
        }
    }
    
    private func setupMakeInitialize() {
        view.backgroundColor = UIColor.black.tt_alpha(0.15)
        
        contentView.cm_shadow(color: UIColor.black, opacity: 0.15, radius: 4, shadowOffset: CGSize(width: 0, height: -4), cornerRadius: 12)
        
        view.layoutIfNeeded()
        headerView.cm_shadow(color: UIColor.black, opacity:0.1, radius: 3, shadowOffset: CGSize(width: 0, height: 3))
        var couponList = [TVCouponCellModel]()
        for _ in 0...10 {
            let coupon = TVCouponCellModel()
            couponList.append(coupon)
        }
        self.coupons = couponList
    }
}
